package org.shan.code.generator;

import org.shan.code.utils.NameUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 生成 mapper
 */
public class MapperGenerator {

    // 指定实体生成所在包的路径
    public static String basePath = new File("").getAbsolutePath();
    // 指定包名
    public static String packageEntity = "org.herui.wushui.entity";
    // 指定包名
    public static String packageOutPath = "org.herui.campus.wushui.dao.mysql";
    // 作者名字
    public String authorName = "shule";
    // 指定需要生成的表的表名，全部生成设置为null
    public String[] generateTables;


    public void generate() throws Exception {

        // 是否有指定生成表，有指定则直接用指定表，没有则全表生成
        List<String> tableNames = new ArrayList<>();
        for (String tableName : generateTables) {
            tableNames.add(tableName);
        }

        PrintWriter pw = null;
        for (int j = 0; j < tableNames.size(); j++) {
            String tableName = tableNames.get(j);
            // 解析生成实体java文件的所有内容
            String content = parse(tableName);
            // 输出生成文件
            String dirName = pkgDirName();
            String javaPath = dirName + "/" + getEntityName(tableName) + "Mapper" + ".java";
            FileWriter fw = new FileWriter(javaPath);
            pw = new PrintWriter(fw);
            pw.println(content);
            pw.flush();
            System.out.println("create class 【" + tableName + "】");
        }
        if (pw != null)
            pw.close();
    }

    /**
     * 功能：获取并创建实体所在的路径目录
     * E:\workspace\idea\lis-server\lis_api\base_api\src\main\java\org\shan\lis\device\entity
     *
     * @return
     */
    private String pkgDirName() {
        String dirName = basePath + "\\src\\main\\java\\" + packageOutPath.replace(".", "\\");
        File dir = new File(dirName);
        if (!dir.exists()) {
            dir.mkdirs();
            System.out.println("mkdirs dir 【" + dirName + "】");
        }
        return dirName;
    }

    /**
     * @description 生成class的所有内容
     */
    private String parse(String tableName) {

        String entityName = getEntityName(tableName);

        StringBuffer sb = new StringBuffer();
        sb.append("package " + packageOutPath + ";\r\n");
        sb.append("\r\n");
        // 判断是否导入工具包
        sb.append("import com.baomidou.mybatisplus.core.mapper.BaseMapper;\r\n\r\n");
        sb.append("import " + packageEntity + "." + entityName + ";\r\n");

        // 注释部分
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sb.append("/**\r\n");
        sb.append(" * author name: " + authorName + "\r\n");
        sb.append(" * create time: " + format.format(new Date()) + "\r\n");
        sb.append(" */ \r\n");

        // 注解部分

        // 实体部分
        String classExtends = "";
        classExtends = " extends BaseMapper<" + entityName + ">";

        sb.append("public interface " + entityName + "Mapper" + classExtends + "{\r\n\r\n");

        sb.append("\r\n");
        sb.append("}\r\n");
        return sb.toString();
    }

    private String getEntityName(String tableName) {
        return NameUtils.under2camel(tableName.substring(2), true);
    }
}
